import java.io.*;
import java.util.*;
public class IndexBuilder {
	File colFp;
	IndexBuilder(String CollectionPathStr){
		colFp = new File(CollectionPathStr);
	}
	public void buildIndex(){
		File postFile = new File(colFp.getPath()+"_postFile.txt");		//File pointer to poting file where all data is present
		File indexDir = new File(colFp.getPath()+"_Index");				//file pointer to the directory were sub index files are present.
		HashMap<SpecialString,IndexWordTile> indexCol = new HashMap<SpecialString,IndexWordTile>(7919);
		IndexWordTile indexWordT;
		
		//posting file
		
		//word Tile
		
		
		File[] subIndexFiles = indexDir.listFiles();
		
		/*for(File f: subIndexFiles){
			System.out.println(f.getPath());
		}*/
		//writing posting file
		try{
			RandomAccessFile postFileRAF = new RandomAccessFile(postFile,"rw");
			
			//traversing all files in index dir
			for(File f : subIndexFiles){
				String word = f.getName();
			//	System.out.println(word);
				SpecialString sString = new SpecialString(word);
				indexWordT = new IndexWordTile(word,postFileRAF.getFilePointer(),sString.hashCode(),0,0);
					//writing data into posting file.
					ArrayList<WordTile> wtArr = new WordListManager().getWordTiles(word, colFp,"old");
					ListIterator<WordTile> wtListIterator = wtArr.listIterator();
					int elements=0;
					while(wtListIterator.hasNext()){
						elements++;
						postFileRAF.writeBytes(wtListIterator.next().toString()+"\n");
						
					}
				indexWordT.setOccurrence(elements);
					
				//stats, no of entries in posting file
				Settings.no_entries_PostFile = Settings.no_entries_PostFile + elements;
				
				
				indexCol.put(sString,indexWordT);
				
			}
			
			/*Iterator it = indexCol.keySet().iterator();
			while(it.hasNext()){
				SpecialString x = (SpecialString) it.next();
				(indexCol.get(x)).display();
			}*/
			
			
			postFileRAF.close();
			
			//stats, entries in index table
			Settings.no_entriesInIndex = indexCol.size();
			
			//stats, hashstats
			HashStats(indexCol);
			//serailizing collection holding main index file
			WordListManager.serializeNow(colFp, indexCol);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		
		
	}
	public void HashStats(HashMap<SpecialString,IndexWordTile> indexCol){
		long avg=0, max =0, noCollisions=0;
		HashSet<Long> collisions = new HashSet<Long>();
		Iterator it = indexCol.keySet().iterator();
		while(it.hasNext()){
			SpecialString x = (SpecialString) it.next();
			IndexWordTile iwt = indexCol.get(x);
			avg = avg + iwt.hashval;
			if(max<iwt.hashval){
				max = iwt.hashval;
			}
			if(!collisions.add(iwt.hashval)){
				noCollisions++;
			}
			
			
		}
		avg = avg / indexCol.size();
		Settings.collision_max = max;
		Settings.collison_avg = avg;
		Settings.collision_no = noCollisions;
	}
	
	
}
